package tech.abing.spider.task.pageprocessor;

import java.net.InetAddress;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.constants.FinalConstants;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.CloseEntity;
import tech.abing.spider.task.model.PayLogClose;
import tech.abing.spider.task.model.PayRecord;
import tech.abing.spider.task.model.TaskTrade;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.RedisUtil;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.SysUtil;

public class PayLogProccessor21 implements PageProcessor {

	private static Logger logger = Logger.getLogger(PayLogProccessor21.class);

	static Pattern normalContain = Pattern.compile("淘宝");
	
	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return normalContain;
	}

	@Override
	public void process(OkPage page, StartContext context, List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		
		String json = page.getContent();
		JSONObject jsonObject = null;
		json = StringUtils.replaceBlank(json);

		jsonObject = JSONObject.fromObject(json);

		String itemId = page.getRequest().getParamsByName("itemId").toString();
		String shopId = page.getRequest().getParamsByName("shopId").toString();
		String type = page.getRequest().getParamsByName("type").toString();
		String catchDate = page.getRequest().getParamsByName("catchDate").toString();
		String lastDate = page.getRequest().getParamsByName("lastDate").toString();
		String monthSale = page.getRequest().getParamsByName("monthSale").toString();

		if (!jsonObject.containsKey("rgv587_flag")) {
			
			logger.info("=========>淘宝成功返回数据");
			
			jsonObject = jsonObject.getJSONObject("data").getJSONObject("showBuyerList");

			Integer curPage = jsonObject.getInt("currentPage");

			JSONArray jsonArray = jsonObject.getJSONArray("data");
			String nickName = "";
			String rank = "";
			String payDate = "";
			String payTime = "";
			String maxPayDate = null;
			String maxPayTime = null;
			PayRecord payRecord = null;
			JSONObject jsonTemp = null;
			String temp = "";

			if (json.contains("SUCCESS")) {
				for (int i = 0; i < jsonArray.size(); i++) {
					jsonTemp = jsonArray.getJSONObject(i);
					
					temp = jsonTemp.getString("payTime");
					payDate = temp.substring(0, 10);
					payTime = temp.substring(10);
					if (DateUtils.compareDate(payDate + " " + payTime, lastDate) == -1) {//之前采集过的不抓取
						return;
					}
					
					payRecord = new PayRecord();
					payRecord.setShopId(shopId);
					payRecord.setPrdNo(itemId);

					payRecord.setMonthSale(monthSale);

					nickName = jsonTemp.getString("buyerNick");
					payRecord.setNickName(nickName);
					if (jsonTemp.containsKey("price")) {
						payRecord.setPayPrice(jsonTemp.getString("price"));
					}
					payRecord.setNum(jsonTemp.getString("amount"));
					
					if (maxPayDate == null) {
						maxPayDate = payDate;
						maxPayTime = payTime;
					}
					if (jsonTemp.containsKey("rateImg")) {
						rank = jsonTemp.getString("rateImg");
						rank = rank.substring(rank.lastIndexOf("/") + 1);
						payRecord.setRank(FinalConstants.rankMap.get(rank));
					}
					if (jsonTemp.containsKey("vip")) {
						payRecord.setLevel(jsonTemp.getString("vip"));
					}
					if (jsonTemp.containsKey("skuInfo")) {
						temp = jsonTemp.getString("skuInfo");
						temp = temp.replace("[", "").replace("\"", "").replace("]", "");
						payRecord.setSku(temp);
					}

					payRecord.setPayDate(payDate);
					payRecord.setPayTime(payTime);

					payRecord.setMaxPayDate(maxPayDate);
					payRecord.setMaxPayTime(maxPayTime);
					payRecord.setType(type);
					payRecord.setCatchDate(DateUtils.getDateTime());
					objectContainer.add(payRecord);
				}
			}
			
			if (curPage == 100) {// 只抓取前100页
				return;
			}
			
			//是否有下一页
			boolean hasNext = jsonObject.getBoolean("hasNext");
			
			if(hasNext){
				
				String preUrl = page.getRequest().getUrl();
				String nextUrl = preUrl.substring(0, preUrl.indexOf("bid_page=")) + "bid_page=" + (curPage + 1);
			
				PageRequest req = context.createPageRequest(nextUrl, PayLogProccessor21.class, 1);
				req.putParams("itemId", itemId);
				req.putParams("shopId", shopId);
				req.putParams("type", type);
				req.putParams("catchDate", catchDate);
				req.putParams("lastDate", lastDate);
				req.putParams("monthSale", monthSale);

				//req.putParams("cookie", SysUtil.getValue("trade.cookie"));
				 //req.putParams("cookie", StringUtils.getRandomCookie());
				 req.putParams("cookie", StringUtils.toString(context.getContextAttribute("cookie")));
				req.putParams("referer", "https://item.taobao.com/item.htm?id=" + itemId);

				req.setUseJsoup(true);
				queue.add(req);// 跟进队列
			}
			
		} else {// 访问受限
			
			//立即返回到redis中
			RedisUtil redisUtil = (RedisUtil)context.getContextAttribute("redisUtil");
			
			//这里catchDate不能用当前时间，因为没有采集成功，下一次采集需要根据catchDate来判断
			TaskTrade taskTrade = new TaskTrade(itemId, shopId, type, lastDate, catchDate, monthSale);

			AtomicInteger atomicInteger = (AtomicInteger)context.getContextAttribute("atomicInteger");
			int count = atomicInteger.incrementAndGet();
			
			CloseEntity closeEntity = new CloseEntity();
			closeEntity.setCloseTime(DateUtils.getDateTime());
			closeEntity.setCount(count);
			
			InetAddress netAddress = InetAddress.getLocalHost();  
			closeEntity.setHostIp(netAddress.getHostAddress());
			closeEntity.setHostName(netAddress.getHostName());

			closeEntity.setProdId(itemId);
			closeEntity.setUrl(page.getRequest().getUrl());
			
			PayLogClose plc = new PayLogClose();
			plc.setTaskTrade(taskTrade);
			plc.setCloseEntity(closeEntity);
			
			String unsaleTask = SysUtil.getValue("redis.task_trade_product_id_unsale");
			String normalTask = SysUtil.getValue("redis.task_trade_product_id_normal");
			String hotTask = SysUtil.getValue("redis.task_trade_product_id_hot");
			
			String closeTask = SysUtil.getValue("redis.close_product_id");
			
				
			TaskTrade tt = plc.getTaskTrade();
			
			int monthSaleInt = StringUtils.toInteger(tt.getMonthSale());
			
			if(monthSaleInt >= FinalConstants.PRODUCT_HOT_SALE_MIN){//热销
				
				//push到制定队列中
				try {
					redisUtil.rpush(tt, hotTask);
					logger.info("访问受限，产品ID["+tt+"]入队队头["+hotTask+"]成功");
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("访问受限，产品ID["+tt+"]入队队头["+hotTask+"]失败", e);
				}
				
			}else if(monthSaleInt >= FinalConstants.PRODUCT_NORMAL_SALE_MIN && monthSaleInt < FinalConstants.PRODUCT_HOT_SALE_MIN){//普通
				
				//push到制定队列中
				try {
					redisUtil.rpush(tt, normalTask);
					logger.info("访问受限，产品ID["+tt+"]入队队头["+normalTask+"]成功");
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("访问受限，产品ID["+tt+"]入队队头["+normalTask+"]失败", e);
				}
				
			}else if(monthSaleInt > FinalConstants.PRODUCT_UNSALE_SALE_MIN && monthSaleInt < FinalConstants.PRODUCT_NORMAL_SALE_MIN){//滞销
				
				//push到制定队列中
				try {
					redisUtil.rpush(tt, unsaleTask);
					logger.info("访问受限，产品ID["+tt+"]入队队头["+unsaleTask+"]成功");
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("访问受限，产品ID["+tt+"]入队队头["+unsaleTask+"]失败", e);
				}
				
			}
			
			//访问受限的日志
			CloseEntity ct = plc.getCloseEntity();
			try {
				redisUtil.lpush(ct, closeTask);
				logger.info("访问受限，日志信息["+ct+"]入队["+closeTask+"]成功");
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("访问受限，日志信息["+ct+"]入队["+closeTask+"]失败", e);
			}
			
			logger.error("访问受限,等待"+FinalConstants.FORBID_WAIT_SECONDS+"秒");
			TimeUnit.SECONDS.sleep(FinalConstants.FORBID_WAIT_SECONDS);
			
		}

	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
